<template>
  <view class="store_page">
    <view class=" _w-max _top _left _z-index-2 _pos-fixed _top _left"
      :style="{ 'background-color': 'rgba(255, 255, 255,' + srollOpacity + ')' }">
      <view :style="{ height: recHeight, width: '100%' }"></view>
      <view class="top_bar">
        <view class="city_selector">
          <picker @change="bindPickerChange" :value="selectIndex" range-key="label" :range="cities">
						<view class="uni-input">{{ city.label || '' }}</view>
					</picker>
          <image class="selection_icon" src="https://taxi.raxan.xyz/wechat/static/images/icon-arrow-down.png" mode=""></image>
        </view>
        <view class="nest_store">附近门店</view>
      </view>
    </view>
    <view class="page_top_bg">
      <view :style="{ height: recHeight, width: '100%' }"></view>
      <view style="height: 352rpx;"></view>
    </view>
    <view :style="{ height: recHeight, width: '100%' }"></view>
    <view :style="{ height: '88rpx', width: '100%' }"></view>
    <view class="store_list">
      <view style="padding: 32rpx;border-radius: 16rpx;background-color: #FFFFFF;margin-bottom: 24rpx;"
        v-for="item in dataSource" :key="item.id">
        <view @click="toDetail(item)" style="height: 52rpx;display: flex;align-items: center;justify-content: space-between;margin-bottom: 24rpx;">
          <view
            style="display: flex;align-items: center;justify-content: flex-start;font-size: 36rpx;font-weight: bold;line-height: 52rpx;">
            {{ item.title }}
            <image style="width: 36rpx;height: 36rpx;" src="https://taxi.raxan.xyz/wechat/static/images/arrow-right_black.png" mode=""></image>
          </view>
          <view style="color: rgba(0, 0, 0, 0.6);font-size: 24rpx;">{{ item.distance }}</view>
        </view>
        <view style="margin-bottom: 24rpx;font-size: 0;">
          <view v-if="!!item.is_store_take" class="tag">门店自取</view>
          <view v-if="!!item.is_door_take" class="tag">送车上门</view>
          <view v-if="!!item.is_free" class="tag" style="background-color: #F8F9FC;color: #FF5700;">免押金</view>
        </view>
        <view class="address-row" @click="previewMap(item)">
          <view class="poiaddress">{{ item.poiaddress }}</view>
          <image style="width: 40rpx;height: 40rpx;flex: 0 0 40rpx" src="https://taxi.raxan.xyz/wechat/static/images/iocn-poiadress.png" mode=""></image>
        </view>
          
      </view>

    </view>
  </view>
</template>
<script>
import { getAllCity } from '@/api/car.js'
import { getNearByStore } from '@/api/store.js'

const CITY_CACHE_KEY = '@@order_city_info';

export default {
  data() {
    return {
      recHeight: 0,
      cities: [],
      srollOpacity: 0,
      city: {},
      location: {},
      pageIsloaded: false,
      dataSource: [],
      selectIndex: 0
    }
  },
  onPageScroll(e) {
    this.srollOpacity = e.scrollTop < 100 && e.scrollTop >= 20 ? '0.' + Math.floor(e.scrollTop) : e.scrollTop < 20 && e.scrollTop >= 0 ? '0.0' + Math.floor(e.scrollTop) : e.scrollTop;
  },
  onShow() {
    if (this.pageIsloaded) {
      this.checkLoading(this.initData)
    }
  },
  onLoad() {
    this.getPhoneInfo();
    this.checkLoading(this.initData)
  },
  methods: {
    checkLoading(fn) {
      uni.showLoading({ title: '加载中' })
      fn().finally(uni.hideLoading)
    },
    bindPickerChange(e) {
      this.city = this.cities[e.detail.value];
      uni.setStorageSync(CITY_CACHE_KEY, { ...this.city });
      this.checkLoading(this.initData);
    },
    toDetail(param) {
      uni.navigateTo({
        url: `/pages/store_sub/detail/index?param=${JSON.stringify(param)}`
      })
    },
    previewMap(param) {
      uni.navigateTo({
        url:`/pages/store_sub/address/index?param=${JSON.stringify(param)}`
      })
    },
    initData() {
      return Promise.all([
        this.getCityList(),
        this.getLocation()
      ]).then(this.getNearByStore).finally(() => {
        this.pageIsloaded = true;
      });
    },
    getNearByStore() {
      return getNearByStore({
        ...this.location,
        city_id: this.city.value
      }).then(res => {
        this.dataSource = res.data || []
      })
    },
    getLocation() {
      return new Promise((resolve, rejet) => {
        uni.getLocation({
          type: 'wgs84',
          fail: rejet,
          success: (res) => {
            this.location = {
              longitude: res.longitude,
              latitude: res.latitude,
            }
            resolve(this.location);
          }
        });
      });
    },
    // 获取顶部高度信息
    getPhoneInfo() {
      uni.getSystemInfo({
        success: (res) => {
          this.recHeight = res.statusBarHeight + 'px'; //获取手机状态栏高度
        }
      });
    },
    getCityList() {
      let that = this;
      return getAllCity().then((res) => {
        that.cities = res.data;
        let city = uni.getStorageSync(CITY_CACHE_KEY);
        if (city) {
          that.city = city;
          this.selectIndex = this.cities.findIndex(val => val.label === city.label);
          this.selectIndex = this.selectIndex < 0 ? 0 : this.selectIndex;
        } else {
          that.cities.forEach((item, index) => {
            if (item.label === '北京市') {
              that.city = { ...item };
              this.selectIndex = index;
              uni.setStorageSync(CITY_CACHE_KEY, { ...item });
            }
          })
        }
      }).catch((err) => {
        uni.showToast({
          title: '城市获取失败',
          icon: 'none'
        })
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.store_page {
  box-sizing: border-box;
  position: relative;
  min-height: 100vh;
  background: #F8F9FC;
  color: rgba(0, 0, 0, 0.9);

  .page_top_bg {
    position: absolute;
    width: 100%;
    left: 0;
    top: 0;
    background: linear-gradient(180deg, rgb(204, 237, 255) 0%, #F5F6FD 100%);
    border-radius: 0px 0px 0px 0px;
    z-index: 0;
  }

  .top_bar {
    height: 88rpx;
    line-height: 88rpx;
    text-align: center;
    position: relative;
    font-weight: 500;
    font-size: 36rpx;
    color: #000000;

    .city_selector {
      position: absolute;
      top: 0;
      left: 32rpx;
      height: 88rpx;
      display: flex;
      align-items: center;
      justify-content: flex-start;
      font-family: PingFang SC, PingFang SC;
      font-weight: 600;
      font-size: 32rpx;
      color: rgba(0, 0, 0, 0.9);
      line-height: 48rpx;
      text-align: left;
      font-style: normal;
      text-transform: none;

      .selection_icon {
        width: 32rpx;
        height: 32rpx;
        margin-left: 8rpx;
      }

    }
  }

  .store_list {
    position: relative;
    margin: 32rpx;
    height: 500rpx;
    z-index: 1;

    .tag {
      display: inline-block;
      padding: 0 16rpx;
      line-height: 44rpx;
      font-size: 28rpx;
      text-align: center;
      background-color: #E6F3FF;
      margin-right: 12rpx;
      border-radius: 8rpx;
    }

    .address-row {
        display: flex;
        align-items: flex-start;
        justify-content: space-between;
        .poiaddress {
          width: 500rpx;
          flex: 0 0 500rpx;
          font-family: PingFang SC, PingFang SC;
          font-weight: 400;
          font-size: 28rpx;
          color: rgba(0,0,0,0.9);
          line-height: 44rpx;
          text-align: left;
          font-style: normal;
          text-transform: none;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
      }
  }
}
</style>